<template>
    <div class="pagination">
        <button :disabled="pageNo === 1" @click="newPageNo -= 1">上一页</button>
        <button v-show="startEnd.start > 1" @click="newPageNo = 1" :class="newPageNo ===1 ? 'active' : ''">1</button>

        <button v-show="startEnd.start > 2">···</button>

        <button v-for="item in pageShow" :key="item" @click="newPageNo = item" :class="newPageNo ===item ? 'active' : ''">{{item}}</button>

        <button v-show="startEnd.end < totalPages - 1 ">···</button>
        <button v-show="startEnd.end < totalPages" @click="newPageNo = totalPages" :class="newPageNo ===totalPages ? 'active' : ''">{{totalPages}}</button>
        <button :disabled="pageNo === totalPages" @click="newPageNo += 1">下一页</button>

        <button style="margin-left: 30px">共 60 条</button>
    </div>
</template>

<script>
export default {
    name: "Pagination",
    props:["pageNo","totalPages","continueNum","changePageNo"],
    data(){
        return{
            newPageNo:this.pageNo
        }
    },
    computed:{
        startEnd() {
            let start = 1;
            let end = 1;
            if(this.totalPages < this.continueNum){
                start = 1;
                end = this.totalPages
            }else {
                start = this.pageNo - Math.floor(this.continueNum /2);
                end = this.totalPages + Math.floor(this.continueNum/2)

                if(start < 1){
                    start = 1;
                    end = this.continueNum
                }
                if(end > this.totalPages){
                    end = this.totalPages
                    start = this.totalPages  - this.continueNum +1
                }
            }
            return {
                start,
                end
            }
        },
        pageShow(){
            const arr = []
            const {start,end} = this.startEnd
            for (let i = start;i<=end;i++){
                arr.push(i)
            }
            return arr
        }
    },
    watch:{
      newPageNo:{
        handler(){
          this.$emit("changePageNo",this.newPageNo)
        }
      }
    }

};
</script>

<style lang="less" scoped>
.pagination {
    button {
        margin: 0 5px;
        background-color: #f4f4f5;
        color: #606266;
        outline: none;
        border-radius: 2px;
        padding: 0 4px;
        vertical-align: top;
        display: inline-block;
        font-size: 13px;
        min-width: 35.5px;
        height: 28px;
        line-height: 28px;
        cursor: pointer;
        box-sizing: border-box;
        text-align: center;
        border: 0;

        &[disabled] {
            color: #c0c4cc;
            cursor: not-allowed;
        }

        &.active {
            cursor: not-allowed;
            background-color: #409eff;
            color: #fff;
        }
    }
}
</style>